<?php

class client
{
	public $username;
	public $name;
	public $email;
	public $ip;
	public $mac;
	public $status;
	public $password;
	protected $auth = false;

	function __construct($username = false)
	{
		$this->username = $username;

		if ($username)
		{

			$sql_client = '
				SELECT `name`, `ip`, `mac`, `status`, `email`
				FROM `clients`
				WHERE `username` = "' .$username. '"';
			$result_client = query($sql_client);

			if (mysql_num_rows($result_client) > 0)
			{
				$client = mysql_fetch_object($result_client);
				mysql_free_result($result_client);
				foreach ($client as $key => $value)
				{
					$this->$key = $value;
				}
			} else {
				$page->warning('{lang_client_not_found}');
			}

		}

	}
	
	function info()
	{
		$result = '
			<client>
				<username>' .$this->username. '</username>
				<name>' .$this->name. '</name>
				<ip>' .$this->ip. '</ip>
				<mac>' .$this->mac. '</mac>
				<status>' .$this->status. '</status>
				<email>' .$this->email. '</email>
			</client>';
	}

	function liberate()
	{
		global $page;

		$exec1 = 'sudo /sbin/iptables -I FORWARD -s ' .$this->ip. ' -m mac --mac-source ' .$this->mac. ' -j ACCEPT';
		$exec2 = 'sudo /sbin/iptables -I INPUT -s ' .$this->ip. ' -m mac --mac-source ' .$this->mac. ' -j ACCEPT';
		$exec3 = 'sudo /sbin/iptables -I OUTPUT -s ' .$this->ip. ' -j ACCEPT';
		
		exec($exec1, $result, $error);
		exec($exec2, $result, $error);
		exec($exec3, $result, $error);
		foreach ($result as $value)
		{
			$page->notify($value);
		}
		if ($error)
		{
			return false;
		}

		return true;
	}

	function block()
	{
		global $page;

		$exec1 = 'sudo iptables -I FORWARD -s ' .$this->ip. ' -m mac --mac-source ' .$this->mac. ' -j DROP';
		$exec2 = 'sudo iptables -I INPUT -s ' .$this->ip. ' -m mac --mac-source ' .$this->mac. ' -j DROP';
		$exec3 = 'sudo iptables -I OUTPUT -s ' .$this->ip. ' -j DROP';
		
		exec($exec1, $result, $error);
		exec($exec2, $result, $error);
		exec($exec3, $result, $error);
		foreach ($result as $value)
		{
			$page->notify($value);
		}
		if ($error)
		{
			return false;
		}

		return true;
	}

	function refresh()
	{
		global $page;
		
		$page->notify($this->status);
		if ($this->status == 'L')
			$this->liberate();
		else
			$this->block();
	}
	
	function insert()
	{
		global $page;

		$sql_check = '
			SELECT * FROM `clients`
				WHERE `username` = "' .$this->username. '"';
		$result_check = query($sql_check);
		if (mysql_num_rows($result_check) > 0)
		{
			$page->error('{lang_user_allready_exists}');
			return false;
		}

		$sql_save = '
			INSERT INTO `clients`
			SET `username` = "' .$this->username. '",
				`password` = "' .$this->password. '",
				`name` = "' .$this->name. '",
				`ip` = "' .$this->ip. '",
				`mac` = "' .$this->mac. '",
				`email` = "' .$this->email. '",
				`status` = "' .$this->status. '"';
		if (!query($sql_save))
		{
			$page->error('{lang_changes_not_saved}');
			return false;
		}
		else
		{
			$page->notify('{lang_changes_saved}');
		}

		$this->refresh();
		return true;
	}
	
	function update()
	{
		global $page;

		$sql_save = '
			UPDATE `clients`
			SET `name` = "' .$this->name. '",
				`ip` = "' .$this->ip. '",
				`mac` = "' .$this->mac. '",
				`status` = "' .$this->status. '",
				`email` = "' .$this->email. '"
			WHERE `username` = "' .$this->username. '"';
		if (!query($sql_save))
		{
			$page->error('{lang_changes_not_saved}');
			return false;
		}
		else
		{
			$page->notify('{lang_changes_saved}');
			return true;
		}
	}
}

?>